﻿using System;
using System.ComponentModel.DataAnnotations;
using TNPTN.Session;
using TNPTN.Lang;
using TNPTN.Constant;
namespace TNPTN.Utility.Validation
{
    /// <summary>
    /// Required Attribute
    /// Author: jabraok
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class tRequiredAttribute : ValidationAttribute
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public tRequiredAttribute()
            : base()
        {
            IgnoreOnEmpty = true;
        }

        public bool IgnoreOnEmpty { get; set; }

        //Override IsValid  
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == default(object) || String.IsNullOrEmpty(value.ToString()) || (IgnoreOnEmpty && String.IsNullOrEmpty(value.ToString().Trim())))
            {
                string message = Lang.Lang.ResourceManager.GetString(Language.VLD_REQUIRE, Info.CurrentCulture);
                message = String.Format(message, validationContext.DisplayName);
                return new ValidationResult(message);
            }

            //Default return - This means there were no validation error  
            return null;
        }
    }
}